package cn.com.anypay.manager.payment.dto;

/**
 * 支付状态枚举
 *
 * 定义支付过程中的各种状态
 *
 * @author AnyPay Team
 * @version 1.0
 * @since 2025-07-05
 */
public enum PaymentStatusDTO {

    /**
     * 待支付
     */
    PENDING("PENDING", "待支付"),

    /**
     * 支付中
     */
    PROCESSING("PROCESSING", "支付中"),

    /**
     * 支付成功
     */
    SUCCESS("SUCCESS", "支付成功"),

    /**
     * 支付失败
     */
    FAILED("FAILED", "支付失败"),

    /**
     * 已取消
     */
    CANCELLED("CANCELLED", "已取消"),

    /**
     * 已关闭
     */
    CLOSED("CLOSED", "已关闭"),

    /**
     * 已退款
     */
    REFUNDED("REFUNDED", "已退款"),

    /**
     * 部分退款
     */
    PARTIAL_REFUNDED("PARTIAL_REFUNDED", "部分退款"),

    /**
     * 未知状态
     */
    UNKNOWN("UNKNOWN", "未知状态");

    /**
     * 状态码
     */
    private final String code;

    /**
     * 状态描述
     */
    private final String description;

    PaymentStatusDTO(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据状态码获取枚举
     */
    public static PaymentStatusDTO fromCode(String code) {
        for (PaymentStatusDTO status : PaymentStatusDTO.values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        return UNKNOWN;
    }

    /**
     * 判断是否为成功状态
     */
    public boolean isSuccess() {
        return this == SUCCESS;
    }

    /**
     * 判断是否为失败状态
     */
    public boolean isFailed() {
        return this == FAILED;
    }

    /**
     * 判断是否为最终状态（不能再变更）
     */
    public boolean isFinal() {
        return this == SUCCESS || this == FAILED || this == CANCELLED || this == CLOSED;
    }

    @Override
    public String toString() {
        return "PaymentStatus{" +
                "code='" + code + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}